package Grade;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * Created by yan_my on 2016/10/13.
 */
public class Grade {
    public Grade(String fileName) throws IOException {
        dataSet = readData(fileName);
    }
    private List<Data> dataSet = null;
    private List<Data> readData(String fileName) throws IOException {
        List<Data> list=new ArrayList<Data>();
        String[] s = null;
        List<Data> dataSet = null;

        BufferedReader bufferReader = new BufferedReader(new InputStreamReader(
                new FileInputStream(fileName)));

        String line = null;
        while ((line = bufferReader.readLine()) != null) {
            s = line.split(",");
            Data data = new Data();
            data.setStudentNum(Integer.parseInt(s[0]));
            data.setName(s[1]);
            sGrades(data);
            System.out.println(data.toString());
            list.add(data);
        }
        return list;

    }

    private void sGrades(Data data) {
        int[] grades = new int[5];
        Random random = new Random();
        for (int i = 0; i < grades.length; i++) {
            grades[i] = 75 + (int) (10 * random.nextGaussian());
            while (grades[i] < 50 || grades[i] > 100) {
                System.out.println("the random is beyond the range.");
                grades[i] = 75 + (int) (10 * random.nextGaussian());
            }
        }
        data.setChinese(grades[0]);
        data.setMath(grades[1]);
        data.setEnglish(grades[2]);
        data.setPhysics(grades[3]);
        data.setChemical(grades[4]);
        int sum = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] ;
        double aver = (double) sum / 5;
        data.setSum(sum);
        data.setAver(aver);
    }


    public void writeTofile() throws IOException {
        String path2 ="./studentScores.txt";
        File file2=new File(path2);
        if (!file2.exists()) {
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileWriter fw2 = new FileWriter(file2, true);
        BufferedWriter bw2 = new BufferedWriter(fw2);
        bw2.write("学号--姓名--语文--数学--英语--物理--化学--总成绩---平均成绩\n");
        for(Data data :dataSet){
            bw2.write(data.toString()+"\n");
        }
        bw2.close();
        fw2.close();

        String path = "./scoresBySort.txt";
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        sort();

        bw.write("学号--姓名--语文--数学--英语--物理--化学--总成绩---平均成绩\n");
        for(Data data :dataSet){
            bw.write(data.toString()+"\n");
        }
        bw.close();
        fw.close();
    }

    private void sort() {
        for (int i = 0; i < dataSet.size(); i++) {
            Data data=dataSet.get(i);
            for (int j = i + 1; j < dataSet.size(); j++) {
                Data temp=null;
                Data data2 = dataSet.get(j);
                if (data.getSum() < data2.getSum()) {
                    temp = data2;
                    data2=data;
                    data = temp;
                    dataSet.set(i,data);
                    dataSet.set(j,data2);
                }
            }
        }
    }
}
